var vm = new Vue({
	el: '#app',
	data: function() {
		return {
			roles: [],
			authorities: [],
			authorityData: [],
			hasAuthority: [],
			role: {},
			dialogAddRoleVisible: false,
			addRole: {
				name: '',
				description: ''
			},

			// 表单校验规则开始
			addRoleRules: {
				name: [{
						required: true,
						message: '请输入角色名',
						trigger: 'blur'
					},
					{
						min: 2,
						max: 20,
						message: '长度在 2到 20个字符',
						trigger: 'blur'
					},
					{
						validator: function(rule, value, callback) {
							if(!isRoleName(value)) {
								callback(new Error('角色名由数字字母或下划线组成'));
							}
							authAxios({
								method: 'get',
								url: CONST_URL + '/role/checkRole',
								params: {
									name: value
								}
							}).then(function(response) {
								if(response.data.code == '0000') {
									if(response.data.data) {
										callback(new Error('角色已存在'));
									} else {
										callback();
									}
								} else {
									console.log(response.data);
								}
							}).catch(function(error) {
								alert(error.response.status);
								console.log(error);
							});
						},
						trigger: 'blur'
					}
				],
				description: [{
						required: true,
						message: '请输入描述',
						trigger: 'blur'
					},
					{
						min: 2,
						max: 20,
						message: '长度在 2到 20个字符',
						trigger: 'blur'
					},
					{
						validator: function(rule, value, callback) {
							if(!isRoleDescription(value)) {
								callback(new Error('描述由中文数字字母或下划线组成'));
							} else {
								callback();
							}
						},
						trigger: 'blur'
					}
				]
			}
			// 表单校验规则结束
		};
	},
	methods: {
		/**
		 * 查询所有角色信息
		 */
		findAll: function() {
			var _this = this;
			return authAxios({
				method: 'get',
				url: CONST_URL + '/role/all'
			}).then(function(response) {
				if(response.data.code == '0000') {
					_this.roles = response.data.data;
				} else {
					_this.$message.error('加载数据失败！');
					console.log(response.data);
				}
			}).catch(function(error) {
				alert(error.response.status);
				console.log(error);
			});
		},

		/**
		 * 查询所有权限信息
		 */
		findAllAuthority: function() {
			var _this = this;
			return authAxios({
				method: 'get',
				url: CONST_URL + '/authority/all'
			}).then(function(response) {
				if(response.data.code == '0000') {
					_this.authorities = response.data.data;
					_this.authorityData = [];
					for(var i = 0; i < _this.authorities.length; i++) {
						_this.authorityData.push({
							key: _this.authorities[i].id,
							label: _this.authorities[i].description
						});
					}
				} else {
					_this.$message.error('加载数据失败！');
					console.log(response.data);
				}
			}).catch(function(error) {
				alert(error.response.status);
				console.log(error);
			});
		},

		/**
		 * 选择角色
		 */
		selectRole: function(role) {
			var _this = this;
			_this.role = JSON.parse(JSON.stringify(role));
			return authAxios({
				method: 'get',
				url: CONST_URL + '/role/findAuthority',
				params: {
					roleId: _this.role.id
				}
			}).then(function(response) {
				_this.hasAuthority = [];
				if(response.data.code == '0000') {
					for(var i = 0; i < response.data.data.length; i++) {
						_this.hasAuthority.push(response.data.data[i].id);
					}
				} else {
					_this.$message.error('加载数据失败！');
					console.log(response.data);
				}
			}).catch(function(error) {
				alert(error.response.status);
				console.log(error);
			});
		},

		/**
		 * 新增角色
		 */
		submitAddRoleForm: function(formName) {
			var _this = this;
			this.$refs[formName].validate(function(valid) {
				if(!valid) {
					console.log('error submit!!');
					return false;
				}
				authAxios({
					method: 'post',
					url: CONST_URL + '/role/add',
					params: _this.addRole
				}).then(function(response) {
					if(response.data.code == '0000') {
						_this.dialogAddRoleVisible = false;
						_this.$message.success('新增成功！');
						_this.findAll();
					} else {
						_this.$message.error('新增失败！');
						console.log(response.data);
					}
				}).catch(function(error) {
					alert(error.response.status);
					console.log(error);
				});
			});
		},

		/**
		 * 修改角色描述
		 */
		updateDescription: function() {
			var _this = this;
			authAxios({
				method: 'post',
				url: CONST_URL + '/role/updateDescription',
				params: _this.role
			}).then(function(response) {
				if(response.data.code == '0000') {
					_this.findAll();
					_this.$message.success('修改成功！');
				} else {
					_this.$message.error('修改角色描述失败！');
					console.log(response.data);
				}
			}).catch(function(error) {
				alert(error.response.status);
				console.log(error);
			});
		},

		/**
		 * 删除角色
		 */
		deleteRole: function() {
			var _this = this;
			this.$confirm('确认删除“' + _this.role.description + '(' + _this.role.name + ')' + '”吗？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(function() {
				authAxios({
					method: 'post',
					url: CONST_URL + '/role/delete',
					params: _this.role
				}).then(function(response) {
					if(response.data.code == '0000') {
						_this.findAll().then(function() {
							_this.selectRole(_this.roles[0]);
						});
						_this.$message.success('删除成功！');
					} else {
						_this.$message.error('删除角色失败！');
						console.log(response.data);
					}
				}).catch(function(error) {
					alert(error.response.status);
					console.log(error);
				});
			}).catch(function() {
				_this.$message({
					type: 'info',
					message: '已取消删除'
				});
			});
		},

		/**
		 * 保存修改
		 */
		saveRole: function() {
			var _this = this;
			this.$confirm('确认保存对“' + _this.role.description + '(' + _this.role.name + ')' + '”的修改吗？', '提示', {
				confirmButtonText: '确定',
				cancelButtonText: '取消',
				type: 'warning'
			}).then(function() {
				authAxios({
					method: 'post',
					url: CONST_URL + '/role/updateRoleAuthority',
					data: {
						roleId: _this.role.id,
						authorityIds: _this.hasAuthority
					}
				}).then(function(response) {
					if(response.data.code == '0000') {
						_this.$message.success('修改成功！');
					} else {
						_this.$message.error('修改角色权限失败！');
						console.log(response.data);
					}
				}).catch(function(error) {
					alert(error.response.status);
					console.log(error);
				});
			}).catch(function() {
				_this.$message({
					type: 'info',
					message: '已取消保存'
				});
			});
		},

		resetForm: function(formName) {
			this.$refs[formName].resetFields();
		}
	},
	mounted: function() {
		this.$nextTick(function() {
			var _this = this;
			checkToken().then(function() {
				var token = localStorage.getItem('token');
				if(token != null) {
					token = JSON.parse(token);
					authAxios.defaults.headers.common['Authorization'] = token.token_type + ' ' + token.access_token;
				}
			}).then(function() {
				_this.findAll().then(function() {
					_this.selectRole(_this.roles[0]);
				});
				_this.findAllAuthority();
			});
		});
	}
});